#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
# author: 小菠萝测试笔记
# blog:  https://www.cnblogs.com/poloyy/
# time: 2021/11/1 4:35 下午
# file: logger_utils.py
"""

import os

from loguru import logger

log = logger

project_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
log_path = f"{project_path}/logs"


def info_only(record):
    return record["level"].name == "INFO" or record["level"].name == "DEBUG"


log.add(
    "%s/runtime_debug_{time}.log" % log_path,
    filter=info_only,
    enqueue=True,
    backtrace=True,
    diagnose=True,
    encoding="utf-8",
    # 一天为周期记录一次日志
    rotation="1 days",
    # 超过一天的文件自动压缩为 zip
    compression="zip",
)  # Once the file is too old, it's rotated


def error_only(record):
    return record["level"].name == "ERROR"


log.add(
    "%s/runtime_error_{time}.log" % log_path,
    filter=error_only,
    enqueue=True,
    backtrace=True,
    diagnose=True,
    encoding="utf-8",
    # 一天为周期记录一次日志
    rotation="1 days",
    # 超过一天的文件自动压缩为 zip
    compression="zip",
)  # Once the file is too old, it's rotated
